<?php
class login extends Controller{
    
    public function __construct(){
        parent::__construct();
    }
    
    public function index(){
        if(IS_POST){
            $mobile=trim($_POST['account']);
            $pwd=trim($_POST['pwd']);
            if(!isset($_POST['code']) || empty($_POST['code'])||$_POST['code']!=$_SESSION['img_code']) {
                $this->ajaxReturn(array('status'=>0,'info'=>'验证码不正确'));
            }
            $where='where is_delete=0 and mobile="'.$mobile.'"';
            $res=db()->getRow('select * from x_jiaxiao_admin '.$where.' limit 1');
            if(!$res) $this->ajaxReturn(array('status'=>0,'info'=>'该账号不存在'));
            if($res['password']&&$res['password']==md5($pwd)) {
                $data['last_time']=date('Y-m-d H:i:s',NOW);
                $data['last_ip']=get_client_ip(0,true);
                db()->update('x_jiaxiao_admin',$data,'id='.$res['id']);
                $_SESSION['client']=$res;
                $school=explode(',',$res['school_id']);
                $_SESSION['client']['jx_id']=$school[0];
                unset($res['password']);
                unset($res['auth_node']);
                unset($res['school_id']);
                insertlogs('login','登录成功('.$_SESSION['client']['name'].')',array(),$res['id']);
                $this->ajaxReturn(array('status'=>1,'data'=>$res));
            }
            else $this->ajaxReturn(array('status'=>0,'info'=>'账号或密码不正确'));
        }
    }
    
    public function image_code(){
        require_once APP_PATH.'lib'.DS.'checkCode.class.php';
        $code=new checkCode();
        $code->code_len = 4;
        $code->width = 130;
        $code->height = 38;
        ob_start();
        $code->doimage();
        $_SESSION['img_code']=$code->get_code();
        $image = ob_get_contents();
        ob_end_clean();
        $base64_image = 'data:png;base64,' . chunk_split(base64_encode($image));
        $this->ajaxReturn(array('status'=>1,'data'=>$base64_image));
    }
   
}